package com.eoe.wiki;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.eoe.wiki.activity.MainActivity;
import com.eoe.wiki.utils.L;

import android.app.Application;

public class WikiApplication extends Application {
	public static WikiApplication application;
	public static StringBuilder wikiHtml;
	public MainActivity mainActivity;
	
	public static WikiApplication getApplication(){
		if(application==null){
			throw new NullPointerException("The application may be not running.");
		}
		return application;
	}
	
	public  MainActivity getMainActivity(){
		return mainActivity;
	}

	public void setMainActivity(MainActivity mainActivity){
		this.mainActivity = mainActivity;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		application = this;
		WikiConfig.initConfig(application);
	}

	@Override
	public void onLowMemory() {
		// TODO Auto-generated method stub
		super.onLowMemory();
	}

	@Override
	public void onTerminate() {
		// TODO Auto-generated method stub
		super.onTerminate();
		L.e("onTerminate");
	}
	
	public static StringBuilder getWikiHtml(){
		if(wikiHtml==null){
			
			try {
				InputStream inpStream = getApplication().getResources().openRawResource(R.raw.html);
				BufferedReader reader =  new BufferedReader(new InputStreamReader(inpStream));
				String line;
				while((line=reader.readLine())!=null){
					wikiHtml.append(line);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				L.e("read html file exception " + e.getMessage());
				throw new NullPointerException("can't read the html file.");
			}
		}
		return wikiHtml;
	}
}
